package com.tigercar.mainbody.controller;

import com.tigercar.common.pojo.PageResult;
import com.tigercar.mainbody.DTO.ServiceDTO;
import com.tigercar.mainbody.DTO.ServiceDetailsDTO;
import com.tigercar.mainbody.DTO.SubjectDTO;
import com.tigercar.mainbody.DTO.SubjectDetailsDTO;
import com.tigercar.mainbody.service.MainBodyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ruiz
 * @date 2021/4/23 10:06
 */
@RestController
public class MainBodyController {

    @Autowired
    private MainBodyService mainBodyService;

    /**
     * 服务商主界面
     */
    @GetMapping("/service")
    public ResponseEntity<PageResult<ServiceDTO>> findAll(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(name = "shortName", required = false) String shortName,
            @RequestParam(name = "number", required = false) Long number,
            @RequestParam(name = "province", required = false) String province,
            @RequestParam(name = "city", required = false) String city,
            @RequestParam(name = "region", required = false) String region,
            @RequestParam(name = "phone", required = false) String phone,
            @RequestParam(name = "isChain", required = false) Boolean isChain,
            @RequestParam(name = "status", required = false) Boolean status
    ) {
        PageResult<ServiceDTO> pageResult = mainBodyService.findAll(page, pageSize, shortName, number, province, city, region, phone, isChain, status);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 配件商主界面
     */
    @GetMapping("/parts")
    public ResponseEntity<PageResult<SubjectDTO>> findAllPart(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(name = "shortName", required = false) String shortName,
            @RequestParam(name = "number", required = false) Long number,
            @RequestParam(name = "status", required = false) Boolean status,
            @RequestParam(name = "contact", required = false) String contact
    ) {
        PageResult<SubjectDTO> result = mainBodyService.findAllParts(page, pageSize, shortName, number, status, contact);
        return ResponseEntity.ok(result);
    }

    /**
     * 锁定
     */
    @PutMapping("/lock/{number}")
    public ResponseEntity<Void> lockSubject(
            @PathVariable("number") Long number
    ) {
        int i = 500;
        if (mainBodyService.lockSubject(true, number)) {
            i = 200;
        }
        return ResponseEntity.status(i).build();
    }

    /**
     * 解锁
     */
    @PutMapping("/unlock/{number}")
    public ResponseEntity<Void> unlockSubject(
            @PathVariable("number") Long number
    ) {
        int i = 500;
        if (mainBodyService.lockSubject(false, number)) {
            i = 200;
        }
        return ResponseEntity.status(i).build();
    }

    // 配件商

    /**
     * 编辑-回显
     */
    @GetMapping("/part/{number}")
    public ResponseEntity<SubjectDetailsDTO> findPartInfo(
            @PathVariable("number") Long number
    ) {
        SubjectDetailsDTO result = mainBodyService.findPartInfo(number);
        return ResponseEntity.ok(result);
    }

    /**
     * 编辑-保存
     */
    @PutMapping("/part/{number}")
    public ResponseEntity<Void> updatePartInfo(

    ) {
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 新增
     */

    // 服务商

    /**
     * 编辑回显
     */
    @GetMapping("/service/{number}")
    public ResponseEntity<ServiceDetailsDTO> findServiceInfo(
            @PathVariable("number") Long number
    ) {
        ServiceDetailsDTO result = mainBodyService.findServiceInfo(number);
        return ResponseEntity.ok(result);
    }
}
